package com.qixin.total;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * Created by Zhang on 2018/8/4.
 * springboot启动类
 */
@SpringBootApplication
@EnableAutoConfiguration
@MapperScan({"com.qixin.part.mapper","com.qixin.api.mapper"}) // mybatis扫描路径，针对的是接口Mapper类//必须加这个，不加报错，如果不加，也可以在每个mapper上添加@Mapper注释，
//配置druid必须加的注解，如果不加，访问页面打不开，filter和servlet、listener之类的需要单独进行注册才能使用
// spring boot里面提供了该注解起到注册作用
@ServletComponentScan(value = "com.qixin.part.config.druid")
//扫描公共模块下的包  和 微信模块下的包  service 和 控制
@ComponentScan(basePackages = {"com.qixin.api","com.qixin.total"})
@EnableCaching // 开启缓存
@EnableScheduling
 public class ToTalApplication extends WebMvcConfigurerAdapter    {
    private static final Logger logger = LoggerFactory.getLogger(ToTalApplication.class);
   //解决后台传long类型精度丢失问题
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter =
                new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);
    }



        public  static  void main(String [] args){
           logger.info("Springboot启动中....");
        SpringApplication.run(ToTalApplication.class,args);
            logger.info("ToTal启动成功！");
    }
}
